package com.link.base.base.pageinfo.controller;


import com.link.base.base.pageinfo.model.PageInfo;
import com.link.base.base.pageinfo.service.PageInfoService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 页面信息请求处理
 */
@Controller
@RequestMapping("/link/pageInfo")
public class PageInfoController extends BasicController<PageInfo> {

    @Resource
    private PageInfoService pageInfoService;

    @Override
    public BasicService<PageInfo> getBasicService() throws Exception {
        return pageInfoService;
    }

    @RequestMapping(value = "/queryUnSelectedByExamplePage")
    @ResponseBody
    public Map<String, Object> queryUnSelectedByExamplePage(@JsonParam QueryParams qps, @JsonParam PageInfo entity, HttpSession session,
                                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            PageInfo t = (PageInfo) BasicModel.transformClass(entity, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<PageInfo> list = pageInfoService.queryUnSelectedByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<PageInfo>());
            result.put("result", e.getMessage());
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
        }
        return result;
    }
}
